/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sda.levelup.calculator.model;

import java.util.ArrayList;
import java.util.StringTokenizer;
import sda.levelup.calculator.Processor;

/**
 *
 * @author user
 */
public class ActionStorage
{

    private ArrayList<UserAction> memory = new ArrayList<UserAction>();

    public ArrayList<UserAction> getMemory() {
        return memory;
    }
    
    public UserAction createAction(String s) {
        UserAction ua = new UserAction();
        Processor pr = new Processor();
        int plus = s.indexOf("+");
        int minus = s.indexOf("-");
        int multiplication = s.indexOf("*");
        int division = s.indexOf("/");
        int result = s.indexOf("=");
        if (plus > 0) {
            ua.setFirstArg(Double.parseDouble(s.substring(0, plus)));
            ua.setOperation(s.charAt(plus));
            ua.setSecondArg(Double.parseDouble(s.substring(plus + 1, result)));
            ua.setResult(ua.getFirstArg() + ua.getSecondArg());
        }
        if (minus > 0) {
            ua.setFirstArg(Double.parseDouble(s.substring(0, minus)));
            ua.setOperation(s.charAt(minus));
            ua.setSecondArg(Double.parseDouble(s.substring(minus + 1, result)));
            ua.setResult(ua.getFirstArg() - ua.getSecondArg());
        }
        if (multiplication > 0) {
            ua.setFirstArg(Double.parseDouble(s.substring(0, multiplication)));
            ua.setOperation(s.charAt(multiplication));
            ua.setSecondArg(Double.parseDouble(s.substring(multiplication + 1, result)));
            ua.setResult(ua.getFirstArg() * ua.getSecondArg());
        }
        if (division > 0) {
            ua.setFirstArg(Double.parseDouble(s.substring(0, division)));
            ua.setOperation(s.charAt(division));
            ua.setSecondArg(Double.parseDouble(s.substring(division + 1, result)));
            ua.setResult(ua.getFirstArg() / ua.getSecondArg());
        }
        return ua;
    }

    public void addAction(UserAction ua) {
        if (memory.size() < 10) {
            memory.add(ua);
        }
        else {
            memory.remove(0);
            memory.add(ua);
        }
    }
}
